package com.domian.news;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.domian.news.adapters.FMViewPageAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPage;
    private NavigationView navigation;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ToolBarDrawerLayoutToggler();
        setNavigationViewListener();
        initMainContent();
    }

    //设置Toolbar同时添加drawerLayout的监听 二者互动
    private void ToolBarDrawerLayoutToggler() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close);
        actionBarDrawerToggle.syncState();
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
    }
    //初始化控件
    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPage = (ViewPager) findViewById(R.id.viewPage);
        navigation = (NavigationView) findViewById(R.id.navigation);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    }
    //初始化菜单的内容
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_toolbar,menu);
        return  true;
    }
    //菜单项选中处理
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.setting:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                return  true;
        }
        return false;
    }

    //设置NavationVie Item 选中项菜单
    private  void setNavigationViewListener(){
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case  R.id.navMenuHome:
                        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        return  true;
                    default:
                        return false;
                }
            }
        });
    }

    private void initMainContent() {
        List<Fragment> fragments = new ArrayList<>();
        Fragment home = HomeFragment.newInstance("博文首页");
        Fragment recommend = RecommendFragment.newInstance("博文推荐");
        Fragment topRead = TopReadFragment.newInstance("阅读排行");
        Fragment blogger = BloggerFragment.newInstance("推荐博客");

        fragments.add(home);
        fragments.add(recommend);
        fragments.add(topRead);
        fragments.add(blogger);

        FMViewPageAdapter adapter=new FMViewPageAdapter(getSupportFragmentManager(),this,fragments);
        viewPage.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPage);
    }
}
